{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4]\n",
      " [ 5  6  7  8  9]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]\n",
      " [20 21 22 23 24]]\n",
      "[[ 5  6  7  8  9]\n",
      " [ 0  1  2  3  4]\n",
      " [10 11 12 13 14]\n",
      " [15 16 17 18 19]\n",
      " [20 21 22 23 24]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "#交换矩阵的其中两行\n",
    "a = np.arange(25).reshape(5,5)\n",
    "print a\n",
    "a[[0,1]] = a[[1,0]]\n",
    "print a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "#找出数组中与给定值最接近的数\n",
    "z = np.array([[0,1,2,3],[4,5,6,7]])\n",
    "a = 5.1\n",
    "print np.abs(z-a).argmin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 2 0 0 1 1 0 2 2]\n",
      " [0 0 2 1 0 2 1 0 1 0]]\n",
      "[ True  True  True  True False  True  True False  True  True]\n"
     ]
    }
   ],
   "source": [
    "#判断二维矩阵中有没有一整列数为0？\n",
    "z = np.random.randint(0,3,(2,10))\n",
    "print z\n",
    "print z.any(axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function randint:\n",
      "\n",
      "randint(...)\n",
      "    randint(low, high=None, size=None)\n",
      "    \n",
      "    Return random integers from `low` (inclusive) to `high` (exclusive).\n",
      "    \n",
      "    Return random integers from the \"discrete uniform\" distribution in the\n",
      "    \"half-open\" interval [`low`, `high`). If `high` is None (the default),\n",
      "    then results are from [0, `low`).\n",
      "    \n",
      "    Parameters\n",
      "    ----------\n",
      "    low : int\n",
      "        Lowest (signed) integer to be drawn from the distribution (unless\n",
      "        ``high=None``, in which case this parameter is the *highest* such\n",
      "        integer).\n",
      "    high : int, optional\n",
      "        If provided, one above the largest (signed) integer to be drawn\n",
      "        from the distribution (see above for behavior if ``high=None``).\n",
      "    size : int or tuple of ints, optional\n",
      "        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then\n",
      "        ``m * n * k`` samples are drawn.  Default is None, in which case a\n",
      "        single value is returned.\n",
      "    \n",
      "    Returns\n",
      "    -------\n",
      "    out : int or ndarray of ints\n",
      "        `size`-shaped array of random integers from the appropriate\n",
      "        distribution, or a single such random int if `size` not provided.\n",
      "    \n",
      "    See Also\n",
      "    --------\n",
      "    random.random_integers : similar to `randint`, only for the closed\n",
      "        interval [`low`, `high`], and 1 is the lowest value if `high` is\n",
      "        omitted. In particular, this other one is the one to use to generate\n",
      "        uniformly distributed discrete non-integers.\n",
      "    \n",
      "    Examples\n",
      "    --------\n",
      "    >>> np.random.randint(2, size=10)\n",
      "    array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])\n",
      "    >>> np.random.randint(1, size=10)\n",
      "    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])\n",
      "    \n",
      "    Generate a 2 x 4 array of ints between 0 and 4, inclusive:\n",
      "    \n",
      "    >>> np.random.randint(5, size=(2, 4))\n",
      "    array([[4, 0, 2, 1],\n",
      "           [3, 2, 2, 0]])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "#生成二维的高斯矩阵\n",
    "help(np.random.randint)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]\n",
      " [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111\n",
      "   0.33333333  0.55555556  0.77777778  1.        ]]\n",
      "[[-1.         -1.         -1.         -1.         -1.         -1.         -1.\n",
      "  -1.         -1.         -1.        ]\n",
      " [-0.77777778 -0.77777778 -0.77777778 -0.77777778 -0.77777778 -0.77777778\n",
      "  -0.77777778 -0.77777778 -0.77777778 -0.77777778]\n",
      " [-0.55555556 -0.55555556 -0.55555556 -0.55555556 -0.55555556 -0.55555556\n",
      "  -0.55555556 -0.55555556 -0.55555556 -0.55555556]\n",
      " [-0.33333333 -0.33333333 -0.33333333 -0.33333333 -0.33333333 -0.33333333\n",
      "  -0.33333333 -0.33333333 -0.33333333 -0.33333333]\n",
      " [-0.11111111 -0.11111111 -0.11111111 -0.11111111 -0.11111111 -0.11111111\n",
      "  -0.11111111 -0.11111111 -0.11111111 -0.11111111]\n",
      " [ 0.11111111  0.11111111  0.11111111  0.11111111  0.11111111  0.11111111\n",
      "   0.11111111  0.11111111  0.11111111  0.11111111]\n",
      " [ 0.33333333  0.33333333  0.33333333  0.33333333  0.33333333  0.33333333\n",
      "   0.33333333  0.33333333  0.33333333  0.33333333]\n",
      " [ 0.55555556  0.55555556  0.55555556  0.55555556  0.55555556  0.55555556\n",
      "   0.55555556  0.55555556  0.55555556  0.55555556]\n",
      " [ 0.77777778  0.77777778  0.77777778  0.77777778  0.77777778  0.77777778\n",
      "   0.77777778  0.77777778  0.77777778  0.77777778]\n",
      " [ 1.          1.          1.          1.          1.          1.          1.\n",
      "   1.          1.          1.        ]]\n",
      "[[ 1.41421356  1.26686158  1.1439589   1.05409255  1.0061539   1.0061539\n",
      "   1.05409255  1.1439589   1.26686158  1.41421356]\n",
      " [ 1.26686158  1.09994388  0.95581392  0.84619701  0.7856742   0.7856742\n",
      "   0.84619701  0.95581392  1.09994388  1.26686158]\n",
      " [ 1.1439589   0.95581392  0.7856742   0.64788354  0.56655772  0.56655772\n",
      "   0.64788354  0.7856742   0.95581392  1.1439589 ]\n",
      " [ 1.05409255  0.84619701  0.64788354  0.47140452  0.35136418  0.35136418\n",
      "   0.47140452  0.64788354  0.84619701  1.05409255]\n",
      " [ 1.0061539   0.7856742   0.56655772  0.35136418  0.15713484  0.15713484\n",
      "   0.35136418  0.56655772  0.7856742   1.0061539 ]\n",
      " [ 1.0061539   0.7856742   0.56655772  0.35136418  0.15713484  0.15713484\n",
      "   0.35136418  0.56655772  0.7856742   1.0061539 ]\n",
      " [ 1.05409255  0.84619701  0.64788354  0.47140452  0.35136418  0.35136418\n",
      "   0.47140452  0.64788354  0.84619701  1.05409255]\n",
      " [ 1.1439589   0.95581392  0.7856742   0.64788354  0.56655772  0.56655772\n",
      "   0.64788354  0.7856742   0.95581392  1.1439589 ]\n",
      " [ 1.26686158  1.09994388  0.95581392  0.84619701  0.7856742   0.7856742\n",
      "   0.84619701  0.95581392  1.09994388  1.26686158]\n",
      " [ 1.41421356  1.26686158  1.1439589   1.05409255  1.0061539   1.0061539\n",
      "   1.05409255  1.1439589   1.26686158  1.41421356]]\n",
      "[[ 0.36787944  0.44822088  0.51979489  0.57375342  0.60279818  0.60279818\n",
      "   0.57375342  0.51979489  0.44822088  0.36787944]\n",
      " [ 0.44822088  0.54610814  0.63331324  0.69905581  0.73444367  0.73444367\n",
      "   0.69905581  0.63331324  0.54610814  0.44822088]\n",
      " [ 0.51979489  0.63331324  0.73444367  0.81068432  0.85172308  0.85172308\n",
      "   0.81068432  0.73444367  0.63331324  0.51979489]\n",
      " [ 0.57375342  0.69905581  0.81068432  0.89483932  0.9401382   0.9401382\n",
      "   0.89483932  0.81068432  0.69905581  0.57375342]\n",
      " [ 0.60279818  0.73444367  0.85172308  0.9401382   0.98773022  0.98773022\n",
      "   0.9401382   0.85172308  0.73444367  0.60279818]\n",
      " [ 0.60279818  0.73444367  0.85172308  0.9401382   0.98773022  0.98773022\n",
      "   0.9401382   0.85172308  0.73444367  0.60279818]\n",
      " [ 0.57375342  0.69905581  0.81068432  0.89483932  0.9401382   0.9401382\n",
      "   0.89483932  0.81068432  0.69905581  0.57375342]\n",
      " [ 0.51979489  0.63331324  0.73444367  0.81068432  0.85172308  0.85172308\n",
      "   0.81068432  0.73444367  0.63331324  0.51979489]\n",
      " [ 0.44822088  0.54610814  0.63331324  0.69905581  0.73444367  0.73444367\n",
      "   0.69905581  0.63331324  0.54610814  0.44822088]\n",
      " [ 0.36787944  0.44822088  0.51979489  0.57375342  0.60279818  0.60279818\n",
      "   0.57375342  0.51979489  0.44822088  0.36787944]]\n"
     ]
    }
   ],
   "source": [
    "x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))\n",
    "print x\n",
    "print y\n",
    "D = np.sqrt(x**2+y**2)\n",
    "print D\n",
    "sigma,mu = 1,0\n",
    "a = np.exp(-(D-mu)**2/(2*sigma**2))\n",
    "print a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
